package cn.iocoder.yudao.module.carbon.controller.admin.gase;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.carbon.controller.admin.gase.vo.*;
import cn.iocoder.yudao.module.carbon.convert.gase.GaseConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.gase.GaseDO;
import cn.iocoder.yudao.module.carbon.service.gase.GaseService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name =  "管理后台 - 温室气体")
@RestController
@RequestMapping("/carbon/gase")
@Validated
public class GaseController {

    @Resource
    private GaseService gaseService;

    @PostMapping("/create")
    @Operation(summary = "创建温室气体")
    @PreAuthorize("@ss.hasPermission('carbon:gase:create')")
    public CommonResult<Long> createGase(@Valid @RequestBody GaseCreateReqVO createReqVO) {
        return success(gaseService.createGase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新温室气体")
    @PreAuthorize("@ss.hasPermission('carbon:gase:update')")
    public CommonResult<Boolean> updateGase(@Valid @RequestBody GaseUpdateReqVO updateReqVO) {
        gaseService.updateGase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除温室气体")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('carbon:gase:delete')")
    public CommonResult<Boolean> deleteGase(@RequestParam("id") Long id) {
        gaseService.deleteGase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得温室气体")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('carbon:gase:query')")
    public CommonResult<GaseRespVO> getGase(@RequestParam("id") Long id) {
        GaseDO gase = gaseService.getGase(id);
        return success(GaseConvert.INSTANCE.convert(gase));
    }

    @GetMapping("/page")
    @Operation(summary = "获得温室气体分页")
    @PreAuthorize("@ss.hasPermission('carbon:gase:query')")
    public CommonResult<PageResult<GaseRespVO>> getGasePage(@Valid GasePageReqVO pageVO) {
        PageResult<GaseDO> pageResult = gaseService.getGasePage(pageVO);
        return success(GaseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出温室气体 Excel")
    @PreAuthorize("@ss.hasPermission('carbon:gase:export')")
    @OperateLog(type = EXPORT)
    public void exportGaseExcel(@Valid GaseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GaseDO> list = gaseService.getGaseList(exportReqVO);
        // 导出 Excel
        List<GaseExcelVO> datas = GaseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "温室气体.xls", "数据", GaseExcelVO.class, datas);
    }

}
